package com.le.buddha.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.le.buddha.common.BusinessRuntimeException;
import com.le.buddha.common.Response;
import com.le.buddha.conf.UserUtil;
import com.le.buddha.mobile.User;
import com.le.buddha.mobile.bo.UserRegisterBO;
import com.le.buddha.mobile.vo.UserInfoVO;
import com.le.buddha.service.IUserService;
import com.le.buddha.service.IWeChatService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * 用户相关接口
 *
 * @author guole
 * @created 2024/11/15
 */
@RestController
@RequestMapping("/app/user")
public class AppUserController {
    @Resource
    private IUserService userService;
    @Resource
    private IWeChatService weChatService;
    /**
     * 注册
     *
     * @return
     */
    @PostMapping("/register")
    Response register(@RequestBody  UserRegisterBO userRegisterBO) {
        if(!PhoneUtil.isMobile(userRegisterBO.getPhone())){
            throw new BusinessRuntimeException("500", "不是有效的手机号，请重新输入");
        }
       Integer count= userService.lambdaQuery().eq(User::getPhone,userRegisterBO.getPhone()).count();
        if(count>0){
            throw new BusinessRuntimeException("500", "手机号已注册");
        }
      String token=  userService.register(userRegisterBO);

        return Response.success(token);
    }

    /**
     * 登录
     *
     * @return
     */
    @PostMapping("/login")
    Response login(@RequestBody  UserRegisterBO userRegisterBO ) {

        String  token=this.userService.login(userRegisterBO);
        return Response.success(token);
    }


    /**
     * 发送验证码
     *
     * @param phone 手机号
     */
    @GetMapping("/sendCaptcha")
    public Response sendCaptcha(HttpServletRequest request, @RequestParam("phone") String phone) {
         Integer count= userService.lambdaQuery().eq(User::getPhone,phone).count();
        Integer type=count==0?1:2;
        userService.sendCaptcha(phone,type);
        return Response.success(type);
    }
    @GetMapping("/registerAndLogin")
    public Response registerAndLogin(@RequestParam("code") String code,
    @RequestParam("source") Integer source, @RequestParam("pid") Long pid){
        if(StrUtil.isEmpty(code)){
            throw new BusinessRuntimeException("500", "code不能为空");

        }
      UserInfoVO userInfoVO=  userService.registerAndLogin(code,source,pid);
        return Response.success(userInfoVO);

    }
    @GetMapping("/getOpenId")
    public Response getOpenId(@RequestParam("code") String code ){
        String token=  weChatService.getOpenId(code);
        return Response.success(token);

    }
    @GetMapping("/getUserInfo")
    public Response getUserInfo(HttpServletRequest request){
        UserInfoVO userInfoVO=null;
        String token= UserUtil.getToken(request);

        User user=  userService.lambdaQuery().eq(User::getToken,token).one();
         if(ObjectUtil.isNotNull(user)){

         }
          userInfoVO= BeanUtil.toBean(user, UserInfoVO.class);
        return Response.success(userInfoVO);

    }
}
